package com.shouxue.dragrvdemo;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;

import androidx.annotation.ColorInt;
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import com.desaysv.dragrvitem.item.ItemData;
import com.desaysv.dragrvitem.item.ItemDrag;
import java.util.Optional;

/**
 * @author uidq2189  ,update COT uidq5175
 * @version 1.0
 * @createDate 2019-12-27 10:57
 * @updateDate 2022-8-18
 * @description 快捷面板的抽象基类
 * 不要在子类里面初始化一些监听 会被注册多次
 */
public abstract class BaseSVShortcutImp implements ISVShortcut, ItemData, ItemDrag {
    private static final String TAG = BaseSVShortcutImp.class.getSimpleName();

    /**
     * 上下文
     */
    private final Context mContext;

    /**
     * 底部 文本
     */
    protected String title = "";

    /**
     * 是否已被添加
     */
    protected boolean isAdded = false;

    /**
     * 是否具有长按事件，默认没有
     */
    protected boolean hasLongClick = false;

    /**
     * 选中状态
     *
     * @see MarqueeSwitch#getMode()
     */
    protected int mode;

    /**
     * 设置序号
     */
    protected int num;

    /**
     * 多状态-跑马灯-选择器
     */
    private MarqueeSwitch mMarqueeSwitch;

    public BaseSVShortcutImp(Context context) {
        this.mContext = context;

    }

    @Override
    public void setMarqueeSwitch(MarqueeSwitch mMarqueeSwitch) {
        Log.d(TAG, "setMarqueeSwitch: WifiShortcut-");
        this.mMarqueeSwitch = mMarqueeSwitch;

        init();
    }

    @Override
    public void init() {
        if (null == mMarqueeSwitch) {
            Log.d(TAG, "init: 快捷按钮失败");
            return;
        }
        Log.i(TAG, "init: 快捷按钮 " + getBaseTitle());
        mMarqueeSwitch
                //默认 包含关闭状态
                .setMin(false)
                .setMaxMode(1)//默认 最大状态
                .setMaxLines(2)//默认显示2行
                .setShowTriangle(false);//默认不带小三角
    }

    @Override
    public int getNum() {
        return num;
    }

    @Override
    public void setNum(int num) {
        this.num = num;
    }

    @Override
    public boolean isAdded() {
        return isAdded;
    }

    @Override
    public void setAdded(boolean isAdded) {
        this.isAdded = isAdded;
    }

    @Override
    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public String getTitle() {
        return title;
    }

    public Context getContext() {
        return mContext;
    }

    /**
     * 获取多状态选择器的对象
     */
    @Override
    public MarqueeSwitch getMarqueeSwitch() {
        return mMarqueeSwitch;
    }

    /**
     * 设置 底部按钮文本
     */
    public void setText(String string) {
        if (null != mMarqueeSwitch) {
            mMarqueeSwitch.setText(string);
        }
    }

    /**
     * 设置 底部按钮文本颜色 - 选中时
     */
    public void setTextSelectColor(@ColorInt int color) {
        if (null != mMarqueeSwitch) {
            mMarqueeSwitch.setTextSelectColor(color);
        }
    }

    /**
     * 设置 底部按钮文本颜色 - 未选中时
     */
    public void setTextColor(@ColorInt int color) {
        if (null != mMarqueeSwitch) {
            mMarqueeSwitch.setTextColor(color);
        }
    }

    /**
     * 设置 底部按钮文本颜色 - 不可用时
     */
    public void setTextDisableColor(@ColorInt int color) {
        if (null != mMarqueeSwitch) {
            mMarqueeSwitch.setTextDisableColor(color);
        }
    }

    /**
     * 不可用时（置灰状态）
     */
    public void setDisableMode(boolean isDisabled) {
        if (null != mMarqueeSwitch) {
            mMarqueeSwitch.setDisableMode(isDisabled);
        }
    }

    /**
     * 判断是否有长按功能
     *
     * @return true表示有
     */
    @Override
    public boolean hasLongClick() {
        return hasLongClick;
    }

    @Override
    public String getBaseTitle() {
        return title;
    }

    @Override
    public void destroy() {
        Optional.ofNullable(mMarqueeSwitch).ifPresent(view -> mMarqueeSwitch = null);

    }

    @Override
    public int getType() {
//        int functionType = getFunctionType();
//        switch (functionType) {
//            case FunctionType.FUNCTION_LDW:
//            case FunctionType.FUNCTION_SPEED_LIMIT_ASSIST:
//            case FunctionType.FUNCTION_INTEL_LAMP_ASSIST:
//            case FunctionType.FUNCTION_FCW:
//            case FunctionType.FUNCTION_RCW:
//            case FunctionType.FUNCTION_CO_DRIVER_SEAT_MASSAGE:
//            case FunctionType.FUNCTION_INTERIOR_ATMOSPHERE_LAMP:
//            case FunctionType.FUNCTION_WIRELESS_CHARGING:
//            case FunctionType.FUNCTION_BSD:
//                return GlobalConstants.ShortcutType.VEHICLE;
//            default:
                return GlobalConstants.ShortcutType.SYSTEM;
//        }
    }

    protected void setChecked(boolean checked) {
        Optional.ofNullable(getMarqueeSwitch()).ifPresent(v -> v.onClick(null));
    }


    @NonNull
    @Override
    public String toString() {
        return "BaseSVShortcutImp{" +
                "isAdded=" + isAdded +
                ", num=" + num +
                '}';
    }

    protected int visibility = View.VISIBLE;
    @Override
    public void setVisibility(int visibility) {
        this.visibility = visibility;
    }

    @Override
    public int getVisibility() {
        return visibility;
    }

    @Override
    public boolean isCanMove() {
        return true;
    }

    @Override
    public boolean isCanChangeRecycler() {
        return true;
    }

    @Override
    public boolean isCanDrag() {
        return true;
    }

}
